class node():
    def __init__(self,val=None,parent=None):
        self.key=val
        self.left=None
        self.right=None
        self.parent=parent
    def insert(self,val):
        if val<self.key:
            if self.left is None:
                self.left=node(val,self)
            else:
                self.left.insert(val)
        else:
            if self.right is None:
                self.right=node(val,self)
            else:
                self.right.insert(val)

class binTree():
    def __init__(self):
        self.root=None
    def insert(self,val):
        if self.root is None:
            self.root=node(val)#equal to node(val,None)
        else:
            self.root.insert(val)
    def printall_stack(self):
        if self.root is None:
            print 'Empty binTree'
        else:
            stk=[self.root]
            while (stk!=[]):
                curr=stk.pop()
                print curr.key,
                if curr.left is not None:
                    stk.append(curr.left)
                if curr.right is not None:
                    stk.append(curr.right)

if __name__=='__main__':
    import random
    tree=binTree()
    for i in random.sample(xrange(50),20):
        tree.insert(i)
    tree.printall_stack()
